import requests
import json


# 请求发送类
class ApiRequest:
    @staticmethod
    def send(
            url,
            method='post',
            data=None,
            requestType='json',
            headers=None,
            cookies=None
    ):
        if method == 'post':
            if requestType == 'json':
                response = requests.post(url, json=data, headers=headers, cookies=cookies)
            else:
                response = requests.post(url, data=data, headers=headers, cookies=cookies)
        elif method == 'get':
            response = requests.post(url, data=data, headers=headers, cookies=cookies)

        result = dict()
        result['code'] = response.status_code
        result['headers'] = response.headers
        # cookies 需要特殊转一下才行
        result['cookies'] =response.cookies.get_dict()
        try:
            result['body'] = response.json()
        except:
            result['body'] = None

        return result
